import 'package:get/get.dart';
import 'package:winkai/entity/role_info.dart';
import 'package:winkai/helper/ses_helper.dart';
import 'package:winkai/helper/user_helper.dart';
import 'package:winkai/main.dart';
import 'package:winkai/route/route_manifest.dart';
import 'package:winkai/service/msg_service.dart';
import 'package:winkai/service/ses_service.dart';
import 'package:winkai/ui/msg/controller/chat_controller.dart';
import 'package:winkai/utils/smart_dialog.dart';
import 'package:winkai/widget/alert_dialog.dart';
import 'package:winkai/widget/option_dialog.dart';

class UserDetailsController extends GetxController {
  late String sessionId;
  late RoleInfo roleInfo;

  UserDetailsController(this.sessionId, this.roleInfo);

  final List<String> _reportReasons = [
    "Spam",
    "Violence",
    "Pornography",
    "Child abuse",
    "Copyright",
    "Personal details",
    "Illegal drugs",
  ];

  void cleanMsg() {
    var context = Get.context;
    if (context == null) return;

    showAlertDialog(
      context,
      message: 'user_clear_msg_tips'.tr,
      confirmText: 'sure'.tr,
      confirmCallback: () async {
        showLoading();
        var tag = 'ChatController-$sessionId';
        Get.find<ChatController>(tag: tag).clearMsg();
        await getIt<MsgService>().deleteBySessionId(sessionId);
        dismissLoading();
        toast('user_clear_complete'.tr);
      },
    );
  }

  void deleteMsg() {
    var context = Get.context;
    if (context == null) return;

    showAlertDialog(
      context,
      message: 'user_delete_chat_tips'.tr,
      confirmText: 'sure'.tr,
      confirmCallback: () async {
        showLoading();
        var sessionInfo =
            await getIt<SesService>().selectSingleOrNull(sessionId);
        if (sessionInfo != null) {
          SesHelper().remove(sessionInfo);
          await getIt<SesService>().deleteById(sessionInfo.session.id);
          UserHelper().subtract(sessionInfo.session.unReadNum);
        }
        var tag = 'ChatController-$sessionId';
        Get.find<ChatController>(tag: tag).clearMsg();
        await getIt<MsgService>().deleteBySessionId(sessionId);
        dismissLoading();
        Get.until((route) => route.settings.name == RouteManifest.main);
      },
    );
  }

  void reportUser() async {
    var context = Get.context;
    if (context == null) return;
    showOptionDialog(
      context,
      options: _reportReasons,
      onOptionClickListener: (reason, index) {
        toast('user_report_complete'.tr);
      },
    );
  }
}
